// 添加链接并记录时间
function addLink() {
    const nameInput = document.getElementById('linkNameInput');
    const name = nameInput.value.trim();
    const input = document.getElementById('linkInput');
    const link = input.value.trim();
    const currentTime = new Date(); // 获取当前时间
    const timeString = currentTime.toLocaleString(); // 将时间转换为本地字符串

    if (name && link) {
        const list = document.getElementById('linksList');
        const listItem = document.createElement('li');
        const linkElement = document.createElement('a');
        linkElement.href = link;
        linkElement.target = '_blank';
        linkElement.innerText = name;
        linkElement.style.marginRight = '10px';
        const timeElement = document.createElement('span');
        timeElement.innerText = ` - 添加时间: ${timeString}`; // 创建时间标签
        const deleteButton = document.createElement('button');
        deleteButton.innerText = '删除';
        deleteButton.onclick = function() {
            if (confirm('确定要删除这个链接吗？')) {
                list.removeChild(listItem);
                // 从localStorage中删除链接
                removeLinkFromLocalStorage(name, link);
            }
        };
        listItem.appendChild(linkElement);
        listItem.appendChild(timeElement);
        listItem.appendChild(deleteButton);
        list.appendChild(listItem);
        addLinkToLocalStorage(name, link, timeString); // 将链接和时间保存到localStorage
        nameInput.value = ''; // 清空名称输入框
        input.value = ''; // 清空链接输入框
    } else {
        alert('请输入链接名称和链接地址！');
    }
}

// 添加链接到localStorage
function addLinkToLocalStorage(name, link, time) {
    let links = JSON.parse(localStorage.getItem('links')) || [];
    links.push({ name, link, time });
    localStorage.setItem('links', JSON.stringify(links));
}

// 从localStorage中删除链接
function removeLinkFromLocalStorage(name, link) {
    let links = JSON.parse(localStorage.getItem('links')) || [];
    links = links.filter(item => item.link !== link);
    localStorage.setItem('links', JSON.stringify(links));
}

// 在页面加载时调用
window.onload = function() {
    loadLinksFromLocalStorage();
};

// 从localStorage读取链接并添加到页面
function loadLinksFromLocalStorage() {
    const links = JSON.parse(localStorage.getItem('links')) || [];
    const list = document.getElementById('linksList');
    links.forEach(item => {
        const listItem = document.createElement('li');
        const linkElement = document.createElement('a');
        linkElement.href = item.link;
        linkElement.target = '_blank';
        linkElement.innerText = item.name;
        linkElement.style.marginRight = '10px';
        const timeElement = document.createElement('span');
        timeElement.innerText = ` - 添加时间: ${item.time}`; // 创建时间标签
        const deleteButton = document.createElement('button');
        deleteButton.innerText = '删除';
        deleteButton.onclick = function() {
            if (confirm('确定要删除这个链接吗？')) {
                list.removeChild(listItem);
                removeLinkFromLocalStorage(item.name, item.link);
            }
        };
        listItem.appendChild(linkElement);
        listItem.appendChild(timeElement);
        listItem.appendChild(deleteButton);
        list.appendChild(listItem);
    });
}
